package com.guigu.tms.work.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.tms.commons.entity.aggregation.CoreOrg;
import com.guigu.tms.commons.entity.base.Truck;
import com.guigu.tms.commons.entity.base.TruckDriver;
import com.guigu.tms.commons.entity.oms.Order;
import com.guigu.tms.commons.entity.oms.OrderCargo;
import com.guigu.tms.commons.entity.work.DriverJob;
import com.guigu.tms.commons.entity.work.TaskTransport;
import com.guigu.tms.commons.entity.work.TransportOrder;
import com.guigu.tms.commons.entity.work.TransportOrderTask;
import com.guigu.tms.work.mapper.*;
import com.guigu.tms.work.service.IDriverJobService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.tms.work.service.ITaskPickupDispatchService;
import com.guigu.tms.work.service.ITruckDriverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 司机作业单 服务实现类
 * </p>
 *
 * @author zwj
 * @since 2022-09-27
 */
@Service
public class DriverJobServiceImpl extends ServiceImpl<DriverJobMapper, DriverJob> implements IDriverJobService {
        @Autowired
        DriverJobMapper mapper;
        @Autowired
        CoreOrgMapper coreOrgMapper;
        @Autowired
        TruckDriverMapper truckDriverMapper;
        @Autowired
        TaskTransportMapper taskTransportMapper;
        @Autowired
        TruckMapper truckMapper;
        @Autowired
        TransportOrderMapper transportOrderMapper;
        @Autowired
        TransportOrderTaskMapper transportOrderTaskMapper;
        @Autowired
        OrderMapper orderMapper;
        @Autowired
        OrderCargoMapper orderCargoMapper;
        @Autowired
        ITruckDriverService truckDriverService;
        @Autowired
        ITaskPickupDispatchService taskPickupDispatchService;
        public PageInfo<DriverJob> queryall(Map<String, String> condition) {
            QueryWrapper<DriverJob> queryWrapper=new QueryWrapper<>();
        for (String key:condition.keySet()) {
            if (!key.equals("pageIndex") && !key.equals("pageSize")) {
                if (!condition.get(key).equals("") && condition.get(key) != null) {
                    if (!key.equals("driverName")) {
                        System.out.println("添加了条件");
                        queryWrapper.eq(key, condition.get(key));
                    } else {
                    }
                }
            }
        }
        int pageIndex = Integer.parseInt(condition.get("pageIndex"));
        int pageSize = Integer.parseInt(condition.get("pageSize"));
        PageHelper.startPage(pageIndex,pageSize);
        List<DriverJob> lists=mapper.selectList(queryWrapper);
        for (DriverJob driverJob:lists) {
            //映射所有的一对一和一对多关系数据
            //起始地
            CoreOrg coreOrg = coreOrgMapper.selectById(driverJob.getStartAgencyId());
            driverJob.setStartAgency(coreOrg);
            //目的地
            driverJob.setEndAgency(coreOrgMapper.selectById(driverJob.getEndAgencyId()));
            //司机信息(查询司机信息 再通过查出来的司机信息查询用户表 查到司机对应的一对一用户关系 最后放进司机对象中)
            TruckDriver truckDriver = truckDriverService.GetAuthUserAndPutInTruckDriver(truckDriverMapper.selectById(driverJob.getDriverId()));
            driverJob.setDriver(truckDriver);
            //运输任务
            TaskTransport taskTransport = taskTransportMapper.selectById(driverJob.getTaskTransportId());
            //车辆信息
            taskTransport.setTruck(truckMapper.selectByIda(taskTransport.getTruckId()));
            //运单（一对多）
            //先查询运单和运输详情的关系表 查出与当前运输详情相对应的运单
            QueryWrapper<TransportOrderTask> wrapper = new QueryWrapper<>();
            wrapper.eq("transport_task_id",taskTransport.getId());
            List<TransportOrderTask> orderTasks = transportOrderTaskMapper.selectList(wrapper);
            //循环查出的关系表数据 再拿到运单id 通过id查询到运单再一个个添加到运输详情的list中
            //新建一个集合
            List<TransportOrder> orders=new ArrayList<>();
            for (TransportOrderTask task:
                    orderTasks) {
                  //通过id查询到单个运单数据 放进集合中
                  TransportOrder torder = transportOrderMapper.Findbyid(task.getTransportOrderId());
                  Order order = orderMapper.selectbyOrderId(torder.getOrderId());
                  QueryWrapper<OrderCargo> wrapper1=new QueryWrapper<>();
                  wrapper1.eq("order_id",order.getOrderId());
                List<OrderCargo> cargos = orderCargoMapper.selectList(wrapper1);
                order.setOrderCargoList(cargos);
                //查询订单对象里的取件信息和派件信息并放入对象 再把订单对象放到运单对象中
                torder.setOrder(taskPickupDispatchService.GetTaskPickupDispatchAndPutInOrder(order));
                orders.add(torder);
            }
            //把集合添加到taskTransport里
            taskTransport.setTransportOrders(orders);
            driverJob.setTaskTransport(taskTransport);
        }
        System.out.println(lists);
        return new PageInfo<DriverJob>(lists);
    }
}
